﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Notification
    /// </summary>
    public class Notification
    {
        private int _NotificationID;

        public int NotificationID
        {
            get
            {
                return _NotificationID;
            }
            set
            {
                _NotificationID = value;
            }
        }
        private int _UserID;

        public int UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }
        private String _Message;

        public String Message
        {
            get
            {
                return _Message;
            }
            set
            {
                _Message = value;
            }
        }
        private Byte _Type;

        public Byte Type
        {
            get
            {
                return _Type;
            }
            set
            {
                _Type = value;
            }
        }
        private DateTime _CreateDate;

        public DateTime CreateDate
        {
            get
            {
                return _CreateDate;
            }
            set
            {
                _CreateDate = value;
            }
        }
        public Notification()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Notification(SqlDataReader SqlReader)
        {
            if (SqlReader != null && !SqlReader.IsClosed)
            {
                _NotificationID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    _UserID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    _Message = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    _Type = SqlReader.GetByte(3);
                }
                if (!SqlReader.IsDBNull(4))
                {
                    _CreateDate = SqlReader.GetDateTime(4);
                }
            }
        }

        public static DataTable GetAll()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "Notification_GetAll";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static DataTable GetTopFive()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "Notification_GetTopFive";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static Notification GetInfo(int NotificationID)
        {
            Notification Notifi = new Notification();
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@notification_id", NotificationID);
            string cmd = "Notification_GetInfo";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (SqlReader.Read())
            {
                Notifi.NotificationID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    Notifi.UserID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    Notifi.Message = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    Notifi.Type = SqlReader.GetByte(3);
                }
                if (!SqlReader.IsDBNull(4))
                {
                    Notifi.CreateDate = SqlReader.GetDateTime(4);
                }
            }
            return Notifi;
        }

        public static int Insert(Notification Notifi)
        {
            DBHelp Dbh = new DBHelp();

            SqlParameter NotificationID = new SqlParameter();
            NotificationID.DbType = DbType.Int32;
            NotificationID.Direction = ParameterDirection.Output;
            NotificationID.ParameterName = @"@notification_id";
            Dbh.AddParameter(NotificationID);
            Dbh.AddParameter("@user_id", Notifi.UserID);
            Dbh.AddParameter("@message", Notifi.Message);
            Dbh.AddParameter("@type", Notifi.Type);
            Dbh.AddParameter("@created_date", Notifi.CreateDate);
            try
            {
                string cmd = "Notification_Insert";
                Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)NotificationID.Value;
            }
            catch (System.Exception)
            {
                return -1;
            }
            
        }

        public static void Update(Notification Notifi)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@notification_id", Notifi.NotificationID);
            Dbh.AddParameter("@user_id", Notifi.UserID);
            Dbh.AddParameter("@message", Notifi.Message);
            Dbh.AddParameter("@type", Notifi.Type);
            Dbh.AddParameter("@created_date", Notifi.CreateDate);

            string cmd = "Notification_Update";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int NotificationID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@notification_id", NotificationID);
            string cmd = "Notification_Delete";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    }
}